package com.qqtech.qquan.user.controller;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.user.enums.UserBankTypeDictEnum;
import com.qqtech.qquan.user.model.UserBank;
import com.qqtech.qquan.user.model.UserBankQuery;
import com.qqtech.qquan.user.model.UserBankVo;
import com.qqtech.qquan.user.service.UserBankService;
import com.qqtech.web.context.BaseWebController;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

@Controller
@RequestMapping(value = "/user/bank")
public class UserBankController extends BaseWebController<UserBank, UserBankQuery> {
	@Resource
	private UserBankService userBankService;

	@Override
	protected BaseService<UserBank> getBaseService() {
		return userBankService;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(@RequestBody UserBank userBank) {
		Result result = new Result();
		userBank.setUserId(super.getUserId());
		Pattern emallPattern = Pattern.compile("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
		Matcher emallMatcher = emallPattern.matcher(userBank.getAccountNo());
		
		Pattern phonePattern = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
		Matcher phoneMatcher = phonePattern.matcher(userBank.getAccountNo());
		
		Matcher mer = Pattern.compile("^[0-9]+$").matcher(userBank.getAccountNo()); 
		if(userBank.getType()==UserBankTypeDictEnum.ALIPAY.getCode()){//支付宝
			if(!emallMatcher.matches()&&!phoneMatcher.matches()){
				result.addErr("支付宝账号必须是邮箱或者手机号码");
			}
		}else{
			if(userBank.getAccountNo().length()<16 || userBank.getAccountNo().length()>20||!mer.matches()){
				result.addErr("银行卡账号必须是16-20的数字");
			}
		}
		userBankService.insert(userBank);
		return result;
	}
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Result detail(@RequestBody UserBank userBank) {
		Result result = new Result();
		UserBankVo detail = userBankService.getById(userBank.getId());
		result.put("detail", detail);
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/update")
	@ResponseBody
	public Result update(@RequestBody UserBank userBank) {
		Result result = new Result();
		userBankService.updateById(userBank);
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/toUpdate")
	@ResponseBody
	public Result toUpdate(@RequestBody UserBank userBank) {
		Result result = new Result();
		UserBankVo toUpdate = userBankService.getById(userBank.getId());
		result.put("toUpdate", toUpdate);
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Result delete(@RequestBody UserBank userBank) {
		Result result = new Result();
		userBankService.deleteById(userBank.getId());
		return result;
	}

	/**
	 * 分页
	 * 
	 * @param userBankQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/managePage")
	@ResponseBody
	public Result managePage(@RequestBody UserBankQuery userBankQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		userBankQuery.setUserId(curUserId);
		Pageable pageKit = new PageKit(userBankQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<UserBankVo> page = userBankService.queryPageList(userBankQuery, pageKit);
		for (UserBankVo userBankVo : page) {
			if(UserBankTypeDictEnum.ALIPAY.getCode()==userBankVo.getType()){
				userBankVo.setTypeStr("支付宝");
			}else if(UserBankTypeDictEnum.BANK.getCode()==userBankVo.getType()){
				userBankVo.setTypeStr("银行卡");
			}
		}
		result.put("page", page);
		return result;
	}
	
	/**
	 * 是否有数据
	 * 
	 * @param userBankQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/hasUserBank")
	@ResponseBody
	public Result hasUserBank(@RequestBody UserBankQuery userBankQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		userBankQuery.setUserId(curUserId);
		int count = userBankService.queryCount(userBankQuery);
		result.put("hasUserBank", count>0?true:false);
		return result;
	}
}
